package main

import (
	"log"
	"os"
	"path/filepath"

	"gitee.com/leonscript/naaru-hammer/v2/hotn"
)

func main() {
	log.Println("=== Stamp Tool 演示程序 ===")

	// 创建输出目录
	outputDir := "output"
	if err := os.MkdirAll(outputDir, 0755); err != nil {
		log.Fatalf("创建输出目录失败: %v", err)
	}

	// 1. 创建测试PDF文件
	testPDF := filepath.Join(outputDir, "90.pdf")
	log.Println("✓ 测试PDF文件创建成功:", testPDF)

	// 2. 测试印章
	stampFile := filepath.Join(outputDir, "seal.png")
	log.Println("✓ 测试印章图片创建成功:", stampFile)

	// 3. 演示普通盖章
	log.Println("\n=== 演示普通盖章 ===")
	normalOutput := filepath.Join(outputDir, "normal_stamp_demo.pdf")
	normalConfig := hotn.NewStampConfig().AddStamp(hotn.Stamp{
		StampFile:     stampFile,
		Position:      "br",
		Offset:        []int32{-50, -50},
		Width:         113,
		Opacity:       0.8,
		PageRangeType: hotn.PageRangeOdd,
	})

	if err := hotn.AddNormalStamp(normalConfig); err != nil {
		log.Printf("❌ 普通盖章失败: %v", err)
	} else {
		log.Println("✓ 普通盖章成功:", normalOutput)
	}

	// 4. 演示骑缝章功能（8页PDF测试重叠效果）
	log.Println("\n=== 演示骑缝章功能（8页重叠测试）===")

	// 创建测试PDF

	ridingOutput := filepath.Join(outputDir, "riding_stamp_8pages_overlapping.pdf")

	ridingConfig := hotn.NewStampConfig().AddStamp(hotn.Stamp{
		StampFile:           stampFile,
		Position:            "tl",
		RidingStampPosition: "l",
		Rotation:            0,
		Offset:              []int32{0, 0},
		Width:               113.38584,
		Height:              113.38584,
		Opacity:             0.8,
		RidingInterval:      2,
		PageRangeType:       hotn.PageRangeAll,
	}).AddStamp(hotn.Stamp{
		StampFile:           stampFile,
		Position:            "bl",
		RidingStampPosition: "b",
		Rotation:            0,
		Offset:              []int32{0, 0},
		Width:               113.38584,
		Height:              113.38584,
		Opacity:             0.8,
		RidingInterval:      2,
		PageRangeType:       hotn.PageRangeOdd,
	})
	ridingConfig.InputFile = testPDF
	ridingConfig.OutputFile = ridingOutput

	if err := hotn.AddRidingStamp(ridingConfig); err != nil {
		log.Printf("❌ 骑缝章失败: %v", err)
	} else {
		log.Println("✓ 骑缝章成功:", ridingOutput)
		log.Println("预期效果：")
		log.Println("- 章1：页面1-3")
		log.Println("- 章2：页面4-6")
		log.Println("- 章3：页面6-8（与章2在第6页重叠）")
		log.Println("- 第6页应该有两个骑缝章")
	}

	// 5. 演示多印章功能
	log.Println("\n=== 演示多印章功能 ===")
	multiStampOutput := filepath.Join(outputDir, "multi_stamp_demo.pdf")

	// 创建多个印章配置
	stamp1 := hotn.NewStamp(stampFile).
		SetPosition("r").
		SetOffset(0, 0).
		SetWidth(113).
		SetOpacity(0.7).
		SetPageRange(hotn.PageRangeOdd) // 奇数页

	stamp2 := hotn.NewStamp(stampFile).
		SetPosition("tr").
		SetOffset(-50, -50).
		SetWidth(113).
		SetOpacity(0.8).
		SetRotation(45).
		SetPageRange(hotn.PageRangeEven) // 偶数页

	stamp3 := hotn.NewStamp(stampFile).
		SetPosition("bc").
		SetOffset(0, 30).
		SetWidth(113).
		SetOpacity(0.6).
		SetRotation(-30).
		SetPageRange(hotn.PageRangeAll) // 所有页面

	multiConfig := hotn.NewStampConfig().
		SetInputFile(testPDF).
		SetOutputFile(multiStampOutput).
		AddStamp(*stamp1).
		AddStamp(*stamp2).
		AddStamp(*stamp3)

	if err := hotn.AddNormalStamp(multiConfig); err != nil {
		log.Printf("❌ 多印章失败: %v", err)
	} else {
		log.Println("✓ 多印章成功:", multiStampOutput)
	}

	// 6. 演示页面选择器
	log.Println("\n=== 演示页面选择器 ===")
	selectorOutput := filepath.Join(outputDir, "selector_demo.pdf")

	selector := hotn.PageSelector{
		PageNumbers: []int{1, 3, 5, 7}, // 指定页面
	}

	selectorConfig := hotn.NewStampConfig().AddStamp(hotn.Stamp{
		StampFile:    stampFile,
		Position:     "c",
		Opacity:      0.9,
		Rotation:     15,
		Width:        113,
		PageSelector: selector,
	})
	selectorConfig.InputFile = testPDF
	selectorConfig.OutputFile = selectorOutput

	if err := hotn.AddNormalStamp(selectorConfig); err != nil {
		log.Printf("❌ 页面选择器演示失败: %v", err)
	} else {
		log.Println("✓ 页面选择器演示成功:", selectorOutput)
	}

	log.Println("\n=== 演示完成 ===")
	log.Println("生成的演示文件:")
	log.Println("- test_document.pdf (原始测试文档)")
	log.Println("- test_stamp.png (测试印章图片)")
	log.Println("- normal_stamp_demo.pdf (普通盖章演示)")
	log.Println("- riding_stamp_demo.pdf (骑缝章演示)")
	log.Println("- multi_stamp_demo.pdf (多印章演示)")
	log.Println("- selector_demo.pdf (页面选择器演示)")
	log.Printf("\n所有文件已保存到: %s\n", outputDir)
	log.Println("请查看生成的PDF文件以验证印章效果！")
}
